<template>
	<view class='center'>
		<view>
			<u-form :model="form" ref="uForm">
				<u-form-item label="标题"><u-input v-model="form.title" placeholder='请输入标题' /></u-form-item>
				<u-form-item label="分类"><u-input v-model="classify1" placeholder='请选择分类' type="select" @click="show = true" />
				<u-action-sheet :list="actionSheetList" v-model="show" @click="actionSheetCallback"></u-action-sheet></u-form-item>
				<u-form-item label="价格"><u-input v-model="form.total" placeholder='请输入价格' type="number"/></u-form-item>
				<u-form-item label="地址"><u-input v-model="form.adress" placeholder='请填写地址' /></u-form-item>
				<u-form-item label="微信"><u-input v-model="form.weixin"  /></u-form-item>
				<u-form-item label="手机"><u-input v-model="form.phone"  /></u-form-item>
				<u-form-item label="简介" ><u-input v-model="form.content" type="textarea"/></u-form-item>
				<u-upload  ref="uUpload" :auto-upload="true"  max-count="1" v-model="form.url" ></u-upload>
				<u-button size="medium" @click="saveTempfile()">上传</u-button>
				
			</u-form>
		</view>
		<view class="button">
			<u-button @click="sumbit">发布闲置</u-button>
		</view>
	</view>
	
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				type:"select",
				imageUrl:'',
				classify1:'',
				form: {
					title: '',
					classify:'',
					total: '',
					adress:'',
					weixin:'',
					phone:'',
					url:'',
					content:'',
					
			},
		
			actionSheetList: [
								{
									value:'0',
									text: '数码产品'
								},
								{
									value:'1',
									text: '家用电器'
								},
								{
									value:'2',
									text: '服装配饰'
								},
								{
									value:'3',
									text: '家居家装'
								},{
									value:'4',
									text: '学习用品',
								},
								{   value:'5',
									text: '运动健身'
								},
								{	
									value:'6',
									text: '美妆个护'
								},
								{	value:'7',
									text: '办公用品'
								},
								{	value:'8',
									text:'母婴用品'
								},
								{	value:'9',
									text: '其他'
								}
							],
		}
	},
	methods:{
		
		//选择图片
		chooseFile(){
			
			uni.chooseMessageFile({
				cont:1,
				success:res=>{
					console.log(res)
					let filename=res.tempFiles.name,
					filePath=res.tempFile.path,
					houzhuiming=this.getHouzhui(filePath,'');
					this.filename=filename;
					if(['jpg','png','jepg'].indexOf(houzhuiming)!=-1){
						this.img=filePath;
						
						uni.previewImage({
							urls:filePath,
						})
						}else{
							this.img='https://img0.baidu.com/it/u=2740500954,3331121224&fm=26&fmt=auto&gp=0.jpg';
						}
						this.saveTempfile(filePath);
						console.log(filePath)
					}
				
			})
		},
		//保存临时文件
		saveTempfile(){
			let files = [];
			files = this.$refs.uUpload.lists
			files.forEach((item,index)=>{
			this.imageUrl=item.url
			})
			
			const result=uniCloud.uploadFile({
			        filePath: this.imageUrl,
			        cloudPath: Date.now()+'.jpg',
			        onUploadProgress: function(progressEvent) {
			                  console.log(progressEvent);
			                  var percentCompleted = Math.round(
			                    (progressEvent.loaded * 100) / progressEvent.total
			                  );
							 }
							 }).then(res=>{
								 this.form.url=res.fileID
								 uni.showToast({
								 	title:"上传成功"
								 })
							 })
		    
		},
		
		// 发布
		sumbit(){
			// 
			// db.collection('opendb-news-articles').add(this.form).then(e=>{
			// 	console.log(this.form.url)
			// })
			
			const db = uniCloud.database();
			 db.collection('opendb-news-articles').add(this.form).then(res=>{
			 	uni.showToast({
			 		title:"发布成功"
			 	})
			  })
			
		},
		//表单classify选择
		actionSheetCallback(index) {
						this.classify1 = this.actionSheetList[index].text;
						this.form.classify=this.actionSheetList[index].value;
					}
	}
	
}
</script>

<style lang="scss">
	
	
</style>
